import request from './index';
import type { ColumnsType, TablePaginationConfig } from 'antd/es/table';

// mock 用户信息
const mockUser = {
  username: 'testuser',
  nickname: '皮蛋用户',
  avatar: 'https://source.unsplash.com/100x100/?avatar',
  isAdmin: false,
};

export const register = (data: FormData) => {
  return request.post('http://localhost:9027/api-service-user/api/app/user/register', data, {
    headers: { 'Content-Type': 'multipart/form-data' }
  });
};

export const login = (data: { username: string; password: string }) => {
  // 真实调用后端登录接口，使用绝对路径
  return request.post('http://localhost:9027/api-service-user/api/app/user/login', data);
};

export const getUserInfo = () => {
  // 模拟获取用户信息
  return Promise.resolve({ data: { code: 0, data: mockUser } });
};

export const logout = () => {
  // 模拟登出
  return Promise.resolve({ data: { code: 0 } });
};

export const fakeLogin = login;

export const updateAvatar = (file: File, userId: number, type: 'admin' | 'app' = 'admin') => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('userId', userId.toString());
  const url = `http://localhost:9027/api-service-user/api/${type}/user/updateAvatar`;
  return request.post(url, formData);
};

export const updateNickname = (nickname: string, type: 'admin' | 'app' = 'admin') => {
  const params = new URLSearchParams();
  params.append('nickname', nickname);
  const url = `http://localhost:9027/api-service-user/api/${type}/user/updateNickname`;
  return request.post(url, params);
};

export const getUserList = (page: number, pageSize: number) => {
  return request.post(`http://localhost:9027/api-service-user/api/admin/user/list/${page}/${pageSize}`);
};

export const queryUserByUsername = (username: string) => {
  return request.post('http://localhost:9027/api-service-user/api/admin/user/queryOne', { username });
};

export const enableUser = (id: number, flag: boolean) => {
  return request.post(`http://localhost:9027/api-service-user/api/admin/user/delete/${id}?flag=${flag}`);
};

export const getUserBalance = () => {
  return request.get('http://localhost:9027/api-service-user/api/app/user/balance');
};

export const recharge = (amount: number) => {
  return request.post('http://localhost:9027/api-service-user/api/app/user/recharge?amount=' + amount);
}; 